<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="80px"
      :inline="false"
      size="samll"
    >
      <el-form-item label="所属课程" prop="courseId">
        <el-select v-model="form.typeId" clearable filterable placeholder="请选择分类" @change="chooseType">
          <el-option label="-请选择分类-" value="" />
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-select v-model="form.courseId" clearable filterable placeholder="请选择课程">
          <el-option label="-请选择课程-" value="" />
          <el-option
            v-for="item in courseOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="知识标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="知识内容" prop="content">
        <el-input type="textarea" :row="4" v-model="form.content"></el-input>
      </el-form-item>

      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import courseKnowledgeApi from "@/api/courseKnowledge";
import typeApi from '@/api/courseType'
import courseDetailApi from '@/api/courseDetail'
export default {
  name: "courseKnowledgeSave",
  props: {
    courseKnowledgeId: {
      type: String,
      default: "",
    },
    close: {
      type: Boolean,
      defalut: false,
    },
  },

  data() {
    return {
      options: [],
      courseOptions:[],
      form: {
        id: "",
        title: "",
        pid: "",
        sort: 0,
      },
      rules: {
        title: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        courseId: [
          { required: true, message: "所属分类不能为空", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    close(newValue, oldValue) {
      this.form = {
        id: "",
        title: "",
        pid: "",
        sort: 0,
      };
    },
    courseKnowledgeId(newValue, oldValue) {
      if (this.courseKnowledgeId) this.getInfo(this.courseKnowledgeId);
    },
  },
  created() {
    if (this.courseKnowledgeId) this.getInfo(this.courseKnowledgeId);
    this.getType()
  },
  methods: {
    // 获取分类
    async getType() {
      let res = await typeApi.list({pageNum:1,pageSize:99})
      if(res.success){
        this.options = res.data.records
      }else{
        this.options = []
      }
    },
    // 根据分类id获取课程列表
    async getCourseList(typeId) {
      let res = await courseDetailApi.list({typeId,pageNum:1,pageSize:99})
      if(res.success){
        this.courseOptions = res.data.records
      }else{
        this.courseOptions = []
      }
    },
    chooseType(data) {
      if(data) this.getCourseList()
    },
    // 获取数据
    async getInfo(id) {
      let res = await courseKnowledgeApi.info(id);
      if (res.success) {
        this.form = res.data.data;
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 保存
    onSubmit() {
      this.$refs.form.validate((v) => {
        if (this.courseKnowledgeId) {
          this.handleUpdate();
        } else {
          this.handleAdd();
        }
      });
    },
    // 添加
    async handleAdd() {
      let res = await courseKnowledgeApi.add(this.form);
      if (res.success) {
        this.$message.success("添加成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },
    // 修改
    async handleUpdate() {
      let res = await courseKnowledgeApi.update(this.form);
      if (res.success) {
        this.$message.success("修改成功");
        this.$emit("done");
      } else {
        return this.$message.error(res.msg);
      }
    },

    handleClose() {
      this.$emit("done");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>